package com.example.zbh.lessona.thread;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.zbh.lessona.R;


/**
 * Created by zbh on 15/11/23.
 */
public class ThreadActivity extends Activity implements View.OnClickListener {
    private Handler handler;
    private Button button, button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        button = (Button) findViewById(R.id.button);
        button1 = (Button) findViewById(R.id.button1);
        button.setOnClickListener(this);
        button1.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                new  MyThread().start();//启动子线程
            break;
            case R.id.button1:
//                send();
                new SecThread().start();
                break;
        }

    }

    private void send() {
        if (handler != null){
            handler.sendEmptyMessage(022);
        }


    }

    public class MyThread extends Thread{
        @Override
        public void run() {
            super.run();
            //子线程构建looper对象
            Looper.prepare();
            handler = new Handler(new Handler.Callback() {
                @Override
                public boolean handleMessage(Message msg) {
                    Log.i("nan",">>>>>>>>>>>>>>>suc" + msg.what);
                    return false;
                }
            });
            //子线程中启动无线循环取消息
            Looper.loop();

        }
    }
    public class SecThread extends Thread{
        @Override
        public void run() {
            super.run();
            if (handler != null){
                handler.sendEmptyMessage(022);
            }
        }
    }
}
